﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class FormMesPlan
    Public usr As String
    Public btnShow As Boolean = False
    Dim objXjPgd As New clsXjPgd
    Dim objPgdMes As New clsPgdMes
    Dim httpHelper As New HttpHelper
    Private Sub FormMesPlan_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        btnSaveMesProjectName.Visible = btnShow
        Dim startDate As String = Date.Now.ToShortDateString()
        txtStartDate.Text = startDate
        Dim endDate As String = Date.Now.ToShortDateString()
        txtEndDate.Text = endDate
        setDgvData()
    End Sub


    Private Sub btnMesSearch_Click(sender As Object, e As EventArgs) Handles btnMesSearch.Click
        setDgvData()
    End Sub

    Private Sub setDgvData()
        If String.IsNullOrEmpty(txtStartDate.Text.Trim()) = False And String.IsNullOrEmpty(txtEndDate.Text.Trim()) = False Then
            Dim dt As DataTable = objXjPgd.getXjPgd(2, txtStartDate.Text.Trim(), txtEndDate.Text.Trim())
            If dt.Rows.Count > 0 Then
                dt.Columns.Add("validity_name")
                For Each row As DataRow In dt.Rows
                    If row("validity") = "0" Then
                        row("validity_name") = "无效"
                    Else
                        row("validity_name") = "有效"
                    End If
                Next
            End If
            dgv.AutoGenerateColumns = False
            dgv.DataSource = dt
        Else
            MsgBox("请选择开始时间和结束时间", MsgBoxStyle.Information, "提示")
        End If
    End Sub

    Private Sub btnMesPlan_Click(sender As Object, e As EventArgs) Handles btnMesPlan.Click
        If String.IsNullOrEmpty(txtStartDate.Text.Trim()) = False And String.IsNullOrEmpty(txtEndDate.Text.Trim()) = False Then
            'Dim params As String = "{""workshop"":""出片"",""date"":""" & "2021-12-08" & """,""compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""}"
            Dim params As String = "{""workshop"":""出片"",""startdate"":""" & DateTime.Parse(txtStartDate.Text.Trim()).ToString("yyyy-MM-dd") & """,""enddate"":""" & DateTime.Parse(txtEndDate.Text.Trim()).ToString("yyyy-MM-dd") & """,""compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""}"
            Dim rtn As String = httpHelper.HttpPostWebService("findDispatchOrderInfoListByTimeAndCJ", params)
            'MsgBox(rtn)
            Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
            Dim code As String = jo("code").ToString() '输出0
            'MsgBox(code)
            Dim data As String = jo("data").ToString() '输出0
            'data = "[{""dispatchingorder"":""PGDH211103001"",
            '""start_time"":""2021-11-02 00:00:00"",
            '""end_time"":""2021-11-02 23:59:59"",
            '""project_code"":""fgm-test-0001"",
            '""project_name"":""fgm-test-0001"",
            '""equip_id"":""070046,070023,070020,070011,"",
            '""jp_type"":""mes不存在"",
            '""prod_spec_id"":""HDR-0.4-201-035-18"",""count"":9.0},
            '{""dispatchingorder"":""PGDH211103222"",""start_time"":""2021-11-02 00:00:00"",
            '""end_time"":""2021-11-02 23:59:59"",
            '""project_code"":""fgm-test-0001"",
            '""project_name"":""fgm-test-0001"",
            '""equip_id"":""070046,070023,070020,070011,"",
            '""jp_type"":""mes不存在"",
            '""prod_spec_id"":""HDR-0.4-201-035-18"",""count"":9.0}]"
            'MsgBox(data)
            Dim jarray As JArray = jarray.Parse(data)
            If jarray.Count > 0 Then
                For index = 0 To jarray.Count - 1
                    Dim en As New entityPgdMes
                    en.pgd_code = jarray(index)("dispatchingorder")
                    en.oper_date = DateTime.Parse(jarray(index)("start_time")).ToShortDateString()
                    en.start_time = jarray(index)("start_time").ToString()
                    en.end_time = DateTime.Parse(jarray(index)("start_time")).AddDays(1).ToString()
                    en.support_model = jarray(index)("p_prod_spec_id")
                    en.type_value = jarray(index)("prod_spec_id")
                    en.mes_project_code = jarray(index)("project_code")
                    en.mes_project_name = jarray(index)("project_name")
                    en.oper_count = jarray(index)("count")
                    en.workshop_id = 2
                    en.validity = "1"
                    en.status = "1"
                    en.origin = "0"
                    en.Ope = usr
                    en.jp_type = String.Empty
                    en.note = String.Empty
                    Dim dtmesdata As DataTable = objPgdMes.getListByPgd(jarray(index)("dispatchingorder").ToString())
                    If dtmesdata.Rows.Count = 0 Then
                        Dim rtn_save As Integer = objPgdMes.insertxjORyzz(en)
                    Else
                        Dim rtn_save As Integer = objPgdMes.updatexjORyzz(en)
                    End If
                Next
                setDgvData()
            Else
                MsgBox("没有数据", MsgBoxStyle.Information, "提示")
            End If
        Else
            MsgBox("请选择开始时间和结束时间", MsgBoxStyle.Information, "提示")
        End If
    End Sub

    Private Sub btnSaveMesProjectName_Click(sender As Object, e As EventArgs) Handles btnSaveMesProjectName.Click
        If dgv.SelectedRows.Count > 0 Then
            CType(Owner, FormJPCheck).mes_project_name = dgv.SelectedRows(0).Cells("mes_project_name").Value.ToString()
            CType(Owner, FormJPCheck).setTxtProjectName()
            Close()
        End If
    End Sub

End Class


Public Class entityPgdMes
    Public pgd_code As String
    Public oper_date As String
    Public mes_project_code As String
    Public mes_project_name As String
    Public support_model As String
    Public type_value As String
    Public oper_count As String
    Public lhj As String
    Public lhj_type As String
    Public note As String
    Public workshop_id As Integer
    Public validity As String
    Public status As String
    Public origin As String
    Public Ope As String
    Public start_time As String
    Public end_time As String
    Public jp_type As String
    Public jp_spec As String
End Class
